package com.shop.cereshop.app.utils;

import cn.hutool.core.collection.CollUtil;
import com.shop.cereshop.commons.domain.calculate.Calculate;
import com.shop.cereshop.commons.domain.shop.CereBusinessTime;
import com.shop.cereshop.commons.enums.ShopTypeEnum;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.shop.cereshop.commons.enums.ShopTypeEnum.*;

/**
 * @description: 业务辅助工具类
 * @author zdd
 * @date 2025/11/3 13:34
 * @version 1.0
 */
public class BusinessUtils {


    /**
     * 获取店铺营业时间
     *
     * @param week           周几
     * @param timeList 店铺营业时间列表
     * return 营业时间 如：00:00~23:59
     */
    public static String getShopBusinessTimeByWeek(String week, List<CereBusinessTime> timeList) {
        if (CollUtil.isEmpty(timeList) || StringUtils.isBlank(week)) {
            return "";
        }
        if (timeList.size() == 1) {
            return timeList.get(0).getWeek() + " " + timeList.get(0).getStartTime() + "-" + timeList.get(0).getEndTime();
        }
        String weekStr = timeList.get(0).getWeek() + "至" + timeList.get(timeList.size() - 1).getWeek();
        for (CereBusinessTime businessTime : timeList) {
            if (businessTime != null && Objects.equals(businessTime.getWeek(), week)) {
                return weekStr + " " + businessTime.getStartTime() + "-" + businessTime.getEndTime();
            }
        }
        return "";
    }


    /**
     * 判断店铺是否24小时营业
     *
     * @param businessTimeByWeek 店铺当天营业时间
     * return 1:24小时营业 0:非24小时营业
     */
    public static Integer judgeShopIsAllDayToUse(String businessTimeByWeek) {
        if (StringUtils.isBlank(businessTimeByWeek)) {
            return 0;
        }
        try {
            // 根据 ~ 分隔
            String[] timeArr = businessTimeByWeek.split("至");
            // 检查数组长度，防止数组越界
            if (timeArr.length < 2) {
                return 0;
            }
            String startTime = timeArr[0];
            String endTime = timeArr[1];

            // 增加空值检查
            if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
                return 0;
            }
            if ("00:00".equals(startTime) && "23:59".equals(endTime)) {
                return 1;
            }
        } catch (Exception e) {
            // 捕获可能的异常，保证方法不会因异常而中断
            return 0;
        }
        return 0;
    }


    /**
     * 判断店铺是否24小时营业
     *
     * @param week           周几
     * @param shopBusinessTimeList 店铺营业时间列表
     * return 1:24小时营业 0:非24小时营业
     */
    public static Integer judgeShopIsAllDayToUse(String week, List<CereBusinessTime> shopBusinessTimeList) {
        if (CollUtil.isEmpty(shopBusinessTimeList) || StringUtils.isBlank(week)) {
            return 0;
        }
        for (CereBusinessTime businessTime : shopBusinessTimeList) {
            if (businessTime != null && Objects.equals(businessTime.getWeek(), week)) {
                if ("00:00".equals(businessTime.getStartTime()) && "23:59".equals(businessTime.getEndTime())) {
                    return 1;
                }
                break;
            }
        }
        return 0;
    }

    /**
     * 判断是否是新店
     *
     * @param createTime 创建时间
     *                   return 1 新店 0 非新店
     */
    public static Integer judgeIsNewShop(long createTime) {
        long now = System.currentTimeMillis();
        long oneWeek = 7 * 24 * 60 * 60 * 1000L;
        if (now - createTime < oneWeek) {
            return 1;
        }
        return 0;
    }

    /**
     * 获取店铺类型：1-同城服务(到店用) 2-线上商城(电商购) 3-同城配送(同城送)
     *
     * @param type          店铺类型
     * @param paramCityCode 参数城市code
     * @param shopAdcode    店铺地址code
     *                      return 店铺类型
     */
    public static Integer getShopType(Integer type, String paramCityCode, String shopAdcode) {
        ShopTypeEnum shopTypeEnum = ShopTypeEnum.getByTypeAndAdcode(type, shopAdcode, paramCityCode);
        if (shopTypeEnum != null) {
            switch (shopTypeEnum) {
                case TONG_CHENG_FU_WU:
                    // 同城服务
                    if (StringUtils.isNotBlank(shopAdcode) && shopAdcode.equals(paramCityCode)) {
                        return TONG_CHENG_FU_WU.getCode();
                    } else {
                        // 表示该店铺只支持同城服务，但又和该用户不在同一个城市就过滤掉
                        return 0;
                    }
                case TONG_CHENG_PEI_SONG:
                    // 同城配送
                    return TONG_CHENG_PEI_SONG.getCode();
                case XIAN_SHANG_SHANG_CHENG:
                    // 线上商城
                    return XIAN_SHANG_SHANG_CHENG.getCode();
            }
        }
        return 0;
    }

    /**
     * 获取店铺服务标签
     *
     * @param calculate 店铺服务标签
     * return 服务标签
     */
    public static List<String> getShopServiceLabels(Calculate calculate) {
        List<String> list = new ArrayList<>();
        if (calculate == null) {
            return list;
        }

        if (isFlagEnabled(calculate.getShopAllWeekToUer())) {
            list.add("全周可用");
        }
        if (isFlagEnabled(calculate.getShopGoodLikeWater())) {
            list.add("好评如潮");
        }
        if (isFlagEnabled(calculate.getHotGoods())) {
            list.add("热销店铺");
        }
        if (isFlagEnabled(calculate.getRefundSafe())) {
            list.add("退款无忧");
        }
        return list;
    }

    /**
     * 获取商品标签
     *
     * @param calculate 商品标签
     * return 商品标签
     */
    public static List<String> getProductLabels(Calculate calculate) {
        List<String> list = new ArrayList<>();
        if (calculate == null) {
            return list;
        }

        if (isFlagEnabled(calculate.getProductAllWeekToUer())) {
            list.add("全周可用");
        }
        if (isFlagEnabled(calculate.getProductGoodLikeWater())) {
            list.add("好评如潮");
        }
        if (isFlagEnabled(calculate.getHotProduct())) {
            list.add("热销商品");
        }
        if (isFlagEnabled(calculate.getPriceGood())) {
            list.add("价格实惠");
        }
        return list;
    }

    public static boolean isFlagEnabled(Integer flag) {
        return flag != null && flag == 1;
    }


}
